




<%@page contentType="text/html" import="java.util.regex.*"
        pageEncoding="UTF-8"%>

<%@page import="javax.jcr.Repository,
        org.apache.jackrabbit.j2ee.RepositoryAccessServlet,
        org.apache.jackrabbit.util.Text,
        javax.jcr.Session,
        javax.jcr.SimpleCredentials,
        javax.jcr.query.Query,
        javax.jcr.query.RowIterator,
        java.text.NumberFormat,
        javax.jcr.query.Row,
        javax.jcr.Node,
        java.net.URLEncoder,
        java.text.SimpleDateFormat,
        java.text.DateFormat,
        java.util.List,
        java.util.ArrayList,
        java.util.Iterator,
        javax.jcr.Value,
        org.apache.jackrabbit.core.version.InternalVersionManager,
        org.apache.jackrabbit.core.version.InternalVersionManagerImpl,
        org.apache.jackrabbit.core.persistence.pool.BundleDbPersistenceManager,
        org.apache.jackrabbit.core.RepositoryImpl,
        org.apache.tika.metadata.Metadata,
        org.apache.tika.parser.AutoDetectParser,
        org.apache.tika.parser.Parser,
        org.apache.tika.sax.BodyContentHandler,
        org.xml.sax.ContentHandler,
        java.io.*,
        javax.jcr.RepositoryException"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">


<%

if (true) {

      String[] s= {"zhou封信", "zhou", "封信"};
      for (String ss: s) {
      Pattern pattern =
            Pattern.compile("\\w{"+ ss.length() + "}");

            Matcher matcher =
            pattern.matcher(ss);

            boolean found = false;
            while (matcher.find()) {

                found = true;
                out.println( ss + "... found");
            }

      }
  return;
  }

    Repository rep;
    Session jcrSession;
    try {
      rep = RepositoryAccessServlet.getRepository(pageContext.getServletContext());
      jcrSession = rep.login(new SimpleCredentials("admin", "admin".toCharArray()));
      Node root = jcrSession.getRootNode();
      if (root.hasNode("mono.doc")) {
        //root.getNode("special-folders").remove();
        //jcrSession.save();
        Node n = root.getNode("mono.doc");
        Node cont = n.getNode("jcr:content");
        getMeta(cont);
        //javax.jcr.Property p = cont.getProperty("jcr:data");
        //out.println(p.getName());
        //java.io.InputStream is = p.getValue().getStream();
        //org.apache.commons.io.IOUtils.copy(is, System.out);
        javax.jcr.NodeIterator ni = n.getNodes();
        while (ni.hasNext()) {
          out.println(ni.nextNode().getPrimaryNodeType().getName());
        }
        out.println("true");
      }
      if (true) {
        return;
      }
      jcrSession.getWorkspace().getVersionManager();
      RepositoryImpl repi = (RepositoryImpl) rep;
      java.lang.reflect.Method method = RepositoryImpl.class.getDeclaredMethod("getVersionManager");
      method.setAccessible(true);
      InternalVersionManagerImpl vm = (InternalVersionManagerImpl) method.invoke(repi);
      BundleDbPersistenceManager pm = (BundleDbPersistenceManager) vm.getPersistenceManager();
      method = BundleDbPersistenceManager.class.getDeclaredMethod("getDataSource");
      method.setAccessible(true);
      javax.sql.DataSource ds = (javax.sql.DataSource) method.invoke(pm);

      method = BundleDbPersistenceManager.class.getDeclaredMethod("getUrl");
      method.setAccessible(true);
      String url = (String) method.invoke(pm);
      System.out.println(url);

      java.sql.Connection con = ds.getConnection();
      java.sql.Statement statement = con.createStatement();
      java.sql.DatabaseMetaData dm = con.getMetaData();
      String sql = "";
      // statement.execute("drop table bookmarks");
      sql = "CREATE TABLE bookmarks ( bookmark_id INT, name varchar(50), uuid VARCHAR(20) NOT NULL, descr VARCHAR (255),time_updated timestamp, PRIMARY KEY (bookmark_id))";
      statement.execute(sql);
      // statement.execute("drop table bookmark_tags");
      sql = "CREATE TABLE bookmark_tags (id INT, bookmark_id INT, tag_id INT, PRIMARY KEY (id))";
      //statement.execute("drop table tags");
      statement.execute(sql);
      sql = " CREATE TABLE tags (tag_id INT,name varchar(50), PRIMARY KEY (tag_id))";
      statement.execute(sql);
      sql = "INSERT INTO bookmarks VALUES(0, 'public', 'admin', 'desc',  current_timestamp) ";
      // sql = "INSERT INTO bookmark_tags values (0, 0, 0)";
      statement.execute(sql);
      //sql = "INSERT INTO tags VALUES (0, 'salt')";
      java.sql.ResultSet rs;
      /*
      java.sql.ResultSet rs = dm.getTables(null, null, "BOOKMARKS", null);
      if (rs.next()) {
      out.println("exists");
      } else {

      statement.execute(sql);

      }
      rs.close();*/
      //statement.execute("insert into tags values ('1', 'mytag')");

      rs = statement.executeQuery("SELECT * from bookmarks");
      while (rs.next()) {
        out.println(rs.getString(1) + "..." + rs.getString(2));
      }
      con.close();
      jcrSession.logout();
    } catch (Throwable e) {
      e.printStackTrace();
      out.println(e);
    }

%>
<%!
  private Metadata getMeta(Node node) throws Exception {
    InputStream is = node.getProperty("jcr:data").getStream();
    ContentHandler contenthandler = new BodyContentHandler(-1);
    Metadata metadata = new Metadata();
    metadata.set(Metadata.RESOURCE_NAME_KEY, node.getName());
    Parser parser = new AutoDetectParser();
    // OOXMLParser parser = new OOXMLParser();
    parser.parse(is, contenthandler, metadata);
    System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE));
    System.out.println("Title: " + metadata.get(Metadata.TITLE));
    System.out.println("Author: " + metadata.get(Metadata.AUTHOR));
    //System.out.println("content: " + contenthandler.toString());
    java.lang.reflect.Field[] f = Metadata.class.getFields();
    for (java.lang.reflect.Field ff: f)
      System.out.println(ff.getName() + ".."+ metadata.get(ff.get(null).toString()));
    return metadata;
  }


 
%>

